import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  mode:'history',//hash模式变history模式,
  routes: [
    {
      path:"/splash",
      name:"splash",
      component:() => import('../components/splash')
    },
    {
      path:"/register",
      name:"register",
      component:() => import('../components/register')
    },
    {
      path:"/login",
      name:"login",
      component:() => import('../components/login')
    },
    {
      path:"/view",
      name:"view",
      component:() => import('../view'),
      children:[
        {
          path:"/",
          redirect:"/home"
        },{
          path:"/home",
          name:"home",
          component:() => import('../view/home')
        },{
          path:"/classify",
          name:"classify",
          component:() => import('../view/classify')
        },{
          path:"/welfare",
          name:"welfare",
          component:() => import('../view/welfare')
        },{
          path:"/bookcase",
          name:"bookcase",
          component:() => import('../view/bookcase')
        },{
          path:"/mine",
          name:"mine",
          component:() => import('../view/mine')
        }
      ]
     
    } 
  ]
})

// 导航守卫
router.beforeEach((to, from, next) => {
    if(to.path === "/login") return next()
    const tokenStr =  window.sessionStorage.getItem('token')
    if(!tokenStr) return next('/login')
    next()
})
export default router
